﻿using UnityEngine;
using System.Collections;
using tools;
using UnityEngine.UI;
using evt;
using UnityEngine.SceneManagement;
public class MainPage : MonoBehaviour {

	public static bool FirstLogin = true;
	public static Login.ResGameStartMsg gamestart = null;
	// Use this for initialization
	private bool DontPlay = false;
	void Start () {
		ServerEventDispather.getInstance ().AddEventDispather (ServerEvent.RE_JOIN_ROOM, ReJoinRoom);
		ZjhView.inZJHGAME = false;
		ZjhSettlement.GameAllOver = false;


		if (BgmPlayer.onplay != "bg_hall") {
			BgmPlayer.play ("bg_hall");
		}
		Debug.Log (info.UserInfo.Version.Trim() +"!="+ gamestart.version.Trim());
		if( FirstLogin ){
			FirstLogin = false;
			if( info.UserInfo.Version.Trim() != gamestart.version.Trim() ){
				DontPlay = true;
			}
		}

		if(!DontPlay){
			InvokeRepeating ("CheckRoom", 0.5f, 0f);
		}
	}

	private void CheckRoom(){
		if( info.UserInfo.UserData != null){
			if ( info.UserInfo.UserData.roomId != 0 ) {
				GSocket.Send (ByteFactory.makeByte (621102,info.UserInfo.UserData.roomId.ToString()));
				info.UserInfo.UserData.roomId = 0;
			}
		}
	}

	private void ReJoinRoom(object obj){
		InvokeRepeating ("CheckRoom2", 1.5f, 0f);
	}

	private void CheckRoom2(){
		GSocket.Send (ByteFactory.makeByte (621102,info.UserInfo.UserData.roomId.ToString()));
	}



	private void GotoUpdateApp(object flag){
		Application.OpenURL("http://fir.im/7ka3");
	}
		
	// Update is called once per frame
	void Update () {
		if ( DontPlay ) {
			ZJHAlert.callback = GotoUpdateApp;
			ServerEventDispather.getInstance().Add(ServerEvent.ERROR_MSG,"您当前的版本过低 点击确定下载新版本");
		}
	}
}
